package com.sky.config;
/**
 * 1.当前模块名：sky-server
 *   模块作用：项目核心服务模块，包含业务逻辑和配置
 * 2.当前包名：com.sky.config
 *   包作用：存放系统配置类，包括Web配置、安全配置等
 * 3.当前类名：WebMvcConfiguration
 *   类作用：Spring MVC全局配置类，定制化Web相关组件
 */
import com.sky.interceptor.JwtTokenAdminInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 *
 * WebMvcConfiguration：Spring MVC核心配置类
 * 知识点说明：
 * 1. 配置类标识：@Configuration注解声明为Spring配置类
 * 2. 日志支持：@Slf4j注解提供日志记录能力
 * 3. 拦截器注册：自定义JWT认证拦截器配置
 * 4. Swagger集成：通过knife4j生成API文档
 * 5. 静态资源映射：配置Swagger UI资源路径
 * 6. 路径匹配规则：精确控制拦截器作用范围
 * 7. 排除路径：开放登录接口无需认证
 * 8. API分组：Docket Bean定义API文档分组
 * 9. 资源处理器：映射Swagger静态资源路径
 * 10. 继承扩展：通过WebMvcConfigurationSupport定制MVC行为
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/employee/login");
    }

    /**
     * 通过knife4j生成接口文档
     * @return
     */
    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("苍穹外卖项目接口文档")
                .version("2.0")
                .description("苍穹外卖项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
